home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Sample Code / Devices / RAMDisk 1.4d5 / RamCDev.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-07  |  12.6 KB  |  485 lines  |  [TEXT/CWIE]

  1. #define __DebugVersion    0
  2. /*
  3. **    Apple Macintosh Developer Technical Support
  4. **
  5. **    RamCDev.c: Ram Disk Control Panel code
  6. **
  7. **    by Gordon Sheridan and Jim Luther
  8. **  modified incessantly by Brian Bechtel
  9. **  and even more so by Quinn "The Eskimo!"
  10. **
  11. **    File:        RamCDev.c
  12. **
  13. **    Copyright © 1992-1996 Apple Computer, Inc.
  14. **    All rights reserved.
  15. **
  16. **    You may incorporate this sample code into your applications without
  17. **    restriction, though the sample code has been provided "AS IS" and the
  18. **    responsibility for its operation is 100% yours.  However, what you are
  19. **    not permitted to do is to redistribute the source as "DTS Sample Code"
  20. **    after having made changes. If you're going to re-distribute the source,
  21. **    we require that you make it clear in the source that the code was
  22. **    descended from Apple Sample Code, but that you've made changes.
  23. **
  24. **    Change History (most recent first):
  25. **
  26. **    Change History (most recent first):
  27. **
  28. **         <1.4>    19970207      Quinn    Reworked to use new file layout and bring generally
  29. **                                    up-to-date.
  30. **         <3>     06/10/94    BL°B    Converted to work with Metrowerks & Symantec
  31. **         <2>     6/13/93    gs        Additional Cleanup
  32. **         <1>     6/13/93    gs        Clean up format.
  33. **        <0+>     10/2/92    gs        Clean up format.
  34. **/
  35.  
  36. #ifdef __MWERKS__
  37. #include <A4Stuff.h>
  38. #endif
  39. #ifdef THINK_C
  40. #include <SetupA4.h>
  41. #endif
  42.  
  43. #include <LowMem.h>
  44. #include <Resources.h>
  45. #include <TextUtils.h>
  46.  
  47. #include "RamDiskCommon.h"
  48.  
  49. ///////////////////////////////////////////////////////////////////////////
  50.  
  51. typedef struct
  52. {
  53.     ConfigRecHandle    config;
  54.     DialogPtr        dlgPtr;
  55.     short            dlgItems;
  56.     long            sizepos;
  57.     long            cursize;
  58.     long            maxsize;
  59.     PicHandle        sizePict;
  60.     Str15            curStr;
  61.     Str15            maxStr;
  62. } RDataStorage, *RDataPtr, **RDataHdl;
  63.  
  64.  
  65. ///////////////////////////////////////////////////////////////////////////
  66. //
  67. // Resource IDs
  68.  
  69.  
  70. enum{
  71.     // DITL
  72.     // cdev's always have the same DITL ID, namely -4064
  73.         iCheckBox = 1, 
  74.         iEditText, 
  75.         iSizeBar, 
  76.         iPict, 
  77.         iMaxSize, 
  78.         iCurSize,
  79.     
  80.     // PICT
  81.     rCDevSizeBarPICT = -4034
  82. };
  83.  
  84. ///////////////////////////////////////////////////////////////////////////
  85. //
  86. // Simple dialog utilites
  87.  
  88. static Handle IGetHand(RDataHdl cdevGlobalsHdl, short item)
  89. {
  90.     Handle    itemHand;
  91.     Rect    itemRect;
  92.     short    itemType;
  93.     
  94.     GetDialogItem( (**cdevGlobalsHdl).dlgPtr, item + (**cdevGlobalsHdl).dlgItems,
  95.                 &itemType, &itemHand, &itemRect);
  96.     return itemHand;
  97. }
  98.  
  99. static void IGetRect(RDataHdl cdevGlobalsHdl, short item, Rect *itemRect)
  100. {
  101.     short    itemType;
  102.     Handle    itemHand;
  103.     
  104.     GetDialogItem( (**cdevGlobalsHdl).dlgPtr, item + (**cdevGlobalsHdl).dlgItems,
  105.                 &itemType, &itemHand, itemRect);
  106. }
  107.  
  108. ///////////////////////////////////////////////////////////////////////////
  109.  
  110. static short UpdateSysz(ConfigRecHandle config)
  111. {
  112.     Handle    sysz;
  113.     short    err = 0;
  114.     
  115.     sysz = Get1Resource ('sysz', 0);
  116.     if (sysz == nil)
  117.     {
  118.         err = ResError();
  119.         if (err == resNotFound || err == 0)    // then make a new one
  120.         {
  121.             sysz = NewHandle(sizeof(long));
  122.             if (sysz == nil)
  123.                 return -1;
  124.             
  125.             **(long **)sysz = 0;
  126.                 
  127.             AddResource(sysz, 'sysz', 0, "\p");
  128.             err =  ResError();
  129.             if (err)
  130.                 return err;
  131.         }
  132.         else
  133.             DebugStr("\pGet1Resource('sysz',0) returned nil...");
  134.     }
  135.     
  136.     if ( (**config).install)
  137.         **(long **)sysz = (16 + (**config).size) * 1024;
  138.     else
  139.         **(long **)sysz = 16 * 1024;
  140.     ChangedResource(sysz);
  141.     WriteResource(sysz);
  142.     ReleaseResource(sysz);
  143.     
  144.     return err;
  145. }
  146.  
  147. ///////////////////////////////////////////////////////////////////////////
  148.  
  149. static RDataHdl  DoInit(DialogPtr CPDialog, short numItems)
  150. {
  151.     short    err;
  152.     Rect    r;
  153.     short    height;
  154.     long    myBufPtr;
  155.     
  156.     RDataHdl    cdevGlobalsHdl = (RDataHdl)NewHandle(sizeof(RDataStorage));
  157.     if (cdevGlobalsHdl == nil)    return cdevGlobalsHdl;
  158.  
  159.     HLock((Handle)cdevGlobalsHdl);
  160.     (**cdevGlobalsHdl).config    = (ConfigRecHandle)Get1Resource ('RDcf', 0);
  161.     err = ResError();
  162.     if ((**cdevGlobalsHdl).config == nil)
  163.     {
  164.         if (err == resNotFound || err == 0)    // then make a new one
  165.         {
  166.             (**cdevGlobalsHdl).config = (ConfigRecHandle)NewHandle(sizeof(ConfigRec));
  167.             // check config
  168.             (**(**cdevGlobalsHdl).config).install = false;
  169.             (**(**cdevGlobalsHdl).config).size     = 0;
  170.             GetIndString((**(**cdevGlobalsHdl).config).volumeName, rStringList, strDefaultNameStr);
  171.             if (ResError() != noErr)
  172.                 BlockMoveData("\pRamDisk",(**(**cdevGlobalsHdl).config).volumeName,8);
  173.             AddResource((Handle)(**cdevGlobalsHdl).config, 'RDcf', 0, "\p");
  174.             // check ResError();
  175.         }
  176.         else
  177.             DebugStr("\pGet1Resource('RDcf',0) returned nil...");
  178.     }
  179.     HLock((Handle)(**cdevGlobalsHdl).config);
  180.  
  181.     (**cdevGlobalsHdl).dlgPtr    = CPDialog;
  182.     (**cdevGlobalsHdl).dlgItems    = numItems;
  183.  
  184.     // initialize with config rsrc
  185.     SetControlValue((ControlHandle)IGetHand(cdevGlobalsHdl,iCheckBox),
  186.                 (**(**cdevGlobalsHdl).config).install);
  187.     (**cdevGlobalsHdl).cursize = (**(**cdevGlobalsHdl).config).size;
  188.     SetDialogItemText(IGetHand(cdevGlobalsHdl,iEditText),(**(**cdevGlobalsHdl).config).volumeName);
  189.     SelectDialogItemText(CPDialog, numItems + iEditText, 0, 999); /* make caret show up */
  190.  
  191.     myBufPtr = (long)LMGetBufPtr();
  192.     (**cdevGlobalsHdl).maxsize    = ( myBufPtr / (1024L*1024L)) * 1024L;
  193.     if ((**cdevGlobalsHdl).maxsize < 0)    (**cdevGlobalsHdl).maxsize = 0;
  194.     NumToString((**cdevGlobalsHdl).maxsize,(**cdevGlobalsHdl).maxStr);
  195.     (**cdevGlobalsHdl).maxStr[++(**cdevGlobalsHdl).maxStr[0]] = 'K';
  196.     
  197.     NumToString((**cdevGlobalsHdl).cursize,(**cdevGlobalsHdl).curStr);
  198.     (**cdevGlobalsHdl).curStr[++(**cdevGlobalsHdl).curStr[0]] = 'K';
  199.  
  200.     IGetRect(cdevGlobalsHdl, iSizeBar, &r);
  201.     height = r.bottom - r.top - 4;
  202.     (**cdevGlobalsHdl).sizepos    = 
  203.         (**cdevGlobalsHdl).cursize/(double)(**cdevGlobalsHdl).maxsize * height;
  204.  
  205.     (**cdevGlobalsHdl).sizePict    = GetPicture (rCDevSizeBarPICT);
  206.     if ((**cdevGlobalsHdl).sizePict == nil)
  207.         DebugStr("\pGetPicture returned nil!");
  208.         
  209.     HUnlock((Handle)(**cdevGlobalsHdl).config);
  210.     HUnlock((Handle)cdevGlobalsHdl);
  211.     
  212.     return cdevGlobalsHdl;
  213. }
  214.  
  215. ///////////////////////////////////////////////////////////////////////////
  216.  
  217. static RDataHdl DoNul(RDataHdl cdevGlobalsHdl)
  218. {
  219.     HLock((Handle)cdevGlobalsHdl);
  220.     HLock((Handle)(**cdevGlobalsHdl).config);
  221.  
  222.     (**(**cdevGlobalsHdl).config).install = 
  223.             GetControlValue((ControlHandle)IGetHand(cdevGlobalsHdl,iCheckBox));
  224.     
  225.     (**(**cdevGlobalsHdl).config).size = (**cdevGlobalsHdl).cursize;
  226.  
  227.     GetDialogItemText( IGetHand(cdevGlobalsHdl,iEditText),
  228.             (**(**cdevGlobalsHdl).config).volumeName);
  229.  
  230.     HUnlock((Handle)(**cdevGlobalsHdl).config);
  231.     HUnlock((Handle)cdevGlobalsHdl);
  232.     
  233.     return cdevGlobalsHdl;
  234. }
  235.  
  236. ///////////////////////////////////////////////////////////////////////////
  237.  
  238. static void DrawItem(RDataHdl cdevGlobalsHdl, short item)
  239. {
  240.     Rect        itemRect,r;
  241.     long        len;
  242.     RgnHandle    rgn = NewRgn();
  243.     
  244.     IGetRect(cdevGlobalsHdl, item, &itemRect);
  245.     SetPort( (**cdevGlobalsHdl).dlgPtr);
  246.     GetClip(rgn);
  247.  
  248.     TextMode(srcCopy);
  249.  
  250.     switch(item)
  251.     {
  252.         case iSizeBar:
  253.             r = itemRect;
  254.             FrameRect(&r);
  255.             r.bottom = r.bottom - (**cdevGlobalsHdl).sizepos;
  256.             ClipRect(&r);
  257.             DrawPicture((**cdevGlobalsHdl).sizePict,&itemRect);
  258.             SetClip(rgn);
  259.             InsetRect(&itemRect,2,2);
  260.             itemRect.top = itemRect.bottom - (**cdevGlobalsHdl).sizepos;
  261.             PaintRect(&itemRect);
  262.             break;
  263.         case iMaxSize:
  264.             NumToString((**cdevGlobalsHdl).maxsize,(**cdevGlobalsHdl).maxStr);
  265.             (**cdevGlobalsHdl).maxStr[++(**cdevGlobalsHdl).maxStr[0]] = 'K';
  266.             len = (**cdevGlobalsHdl).maxStr[0];
  267.             TETextBox (&(**cdevGlobalsHdl).maxStr[1], len, &itemRect, teJustRight);                        
  268.             break;
  269.         case iCurSize:    
  270.             NumToString((**cdevGlobalsHdl).cursize,(**cdevGlobalsHdl).curStr);
  271.             (**cdevGlobalsHdl).curStr[++(**cdevGlobalsHdl).curStr[0]] = 'K';
  272.             len = (**cdevGlobalsHdl).curStr[0];
  273.             TETextBox (&(**cdevGlobalsHdl).curStr[1], len, &itemRect, teJustRight);                        
  274.             break;
  275.     }
  276.      
  277.     TextMode(srcOr);
  278. }
  279.  
  280. ///////////////////////////////////////////////////////////////////////////
  281.  
  282. static RDataHdl DoHit(RDataHdl cdevGlobalsHdl, short item)
  283. {
  284.     short    value,oldSize,newSize, barHeight;
  285.     Point    mouse;
  286.     Rect    r;
  287.     
  288.     switch(item)
  289.     {
  290.         case iCheckBox:
  291.             value = GetControlValue((ControlHandle)IGetHand(cdevGlobalsHdl,item));
  292.             SetControlValue((ControlHandle)IGetHand(cdevGlobalsHdl,item),!value);
  293.             (**(**cdevGlobalsHdl).config).install = !value;
  294.             break;
  295.         case iSizeBar:
  296.             oldSize = (**cdevGlobalsHdl).sizepos;
  297.             IGetRect(cdevGlobalsHdl,iSizeBar,&r);
  298.             barHeight = r.bottom - r.top - 4;
  299.             while(StillDown())
  300.             {
  301.                 GetMouse(&mouse);
  302.                 IGetRect(cdevGlobalsHdl,iSizeBar,&r);
  303.                 InsetRect(&r,0,2);
  304.                 if (PtInRect(mouse,&r))
  305.                 {
  306.                     newSize = r.bottom - mouse.v;
  307.                     if ((newSize < (**cdevGlobalsHdl).sizepos) ||
  308.                         (newSize > (**cdevGlobalsHdl).sizepos))
  309.                     {
  310.                         (**cdevGlobalsHdl).sizepos = newSize;
  311.                         (**cdevGlobalsHdl).cursize =
  312.                             newSize/ (double)barHeight * 
  313.                             (**cdevGlobalsHdl).maxsize;
  314.                         (**cdevGlobalsHdl).cursize =
  315.                             ((**cdevGlobalsHdl).cursize/32) * 32;
  316.                         DrawItem(cdevGlobalsHdl,iSizeBar);
  317.                         DrawItem(cdevGlobalsHdl,iCurSize);
  318.                     }
  319.                 }
  320.                 else
  321.                 {
  322.                     if ((**cdevGlobalsHdl).sizepos != oldSize)
  323.                     {
  324.                         (**cdevGlobalsHdl).sizepos = oldSize;
  325.                         (**cdevGlobalsHdl).cursize =
  326.                             oldSize / (double)barHeight * 
  327.                             (**cdevGlobalsHdl).maxsize;
  328.                         (**cdevGlobalsHdl).cursize =
  329.                             ((**cdevGlobalsHdl).cursize/32) * 32;
  330.                         DrawItem(cdevGlobalsHdl,iSizeBar);
  331.                         DrawItem(cdevGlobalsHdl,iCurSize);
  332.                     }
  333.                 }
  334.             }
  335.             break;
  336.     }
  337.     return cdevGlobalsHdl;
  338. }
  339.  
  340. ///////////////////////////////////////////////////////////////////////////
  341.  
  342. static RDataHdl DoClose(RDataHdl cdevGlobalsHdl)
  343. {
  344.     short    err;
  345.     
  346.     if (cdevGlobalsHdl)
  347.     {
  348.         if ( (**cdevGlobalsHdl).config)
  349.         {
  350.             err = UpdateSysz((**cdevGlobalsHdl).config);
  351.         
  352.             HLock((Handle)cdevGlobalsHdl);
  353.             ChangedResource((Handle)(**cdevGlobalsHdl).config);
  354.             WriteResource((Handle)(**cdevGlobalsHdl).config);
  355.             ReleaseResource((Handle)(**cdevGlobalsHdl).config);
  356.             // check for ResError()!!!
  357.             HUnlock((Handle)cdevGlobalsHdl);
  358.         }
  359.     
  360.         DisposeHandle((Handle)cdevGlobalsHdl);
  361.         cdevGlobalsHdl = nil;
  362.     }
  363.     return cdevGlobalsHdl;
  364. }
  365.  
  366. ///////////////////////////////////////////////////////////////////////////
  367.  
  368. static RDataHdl DoUpdate(RDataHdl cdevGlobalsHdl)
  369. {
  370.     DrawItem(cdevGlobalsHdl, iSizeBar);
  371.     DrawItem(cdevGlobalsHdl, iMaxSize);
  372.     DrawItem(cdevGlobalsHdl, iCurSize);
  373.     
  374.     return cdevGlobalsHdl;
  375. }
  376.  
  377. ///////////////////////////////////////////////////////////////////////////
  378.  
  379. static RDataHdl DoActive(RDataHdl cdevGlobalsHdl)
  380. {
  381.     return cdevGlobalsHdl;
  382. }
  383.  
  384. static RDataHdl DoDeactive(RDataHdl cdevGlobalsHdl)
  385. {
  386.     return cdevGlobalsHdl;
  387. }
  388.  
  389. ///////////////////////////////////////////////////////////////////////////
  390.  
  391. static RDataHdl DoKeyEvent(RDataHdl cdevGlobalsHdl,
  392.                  DialogPtr CPDialog,
  393.                  EventRecord *event)
  394. {
  395.     char    tempChar;
  396.     
  397.     tempChar = event->message & charCodeMask;    // get the character, and check
  398.     if (event->modifiers & cmdKey)                //  status of command key
  399.     {            
  400.         event->what = nullEvent;                // and empty event type
  401.         
  402.         switch (tempChar)                        // set appropriate message
  403.         {
  404.             case 'X':
  405.             case 'x':    DialogCut(CPDialog);    break;
  406.             
  407.             case 'C':
  408.             case 'c':    DialogCopy(CPDialog);    break;
  409.             
  410.             case 'V':
  411.             case 'v':    DialogPaste(CPDialog);    break;
  412.         }
  413.     }
  414.     return cdevGlobalsHdl;
  415. }
  416.  
  417. ///////////////////////////////////////////////////////////////////////////
  418.  
  419. extern pascal RDataHdl main (short message, short item, short numItems, short CPanelID,
  420.                          EventRecord *theEvent, RDataHdl cdevGlobalsHdl, DialogPtr CPDialog);
  421.  
  422. extern pascal RDataHdl main (short message, short item, short numItems, short CPanelID,
  423.                          EventRecord *theEvent, RDataHdl cdevGlobalsHdl, DialogPtr CPDialog)
  424. {
  425. #pragma unused (CPanelID)        /* unused formal parameters */
  426.  
  427. #ifdef THINK_C
  428.     RememberA0();
  429.     SetUpA4();
  430. #endif
  431. #ifdef __MWERKS__
  432.     long oldA4=SetCurrentA4();
  433. #endif
  434.  
  435.     if (message == macDev) return((RDataHdl) 1);                /* we work on every machine */
  436.     else if (cdevGlobalsHdl != nil)
  437.     {
  438.         switch(message)
  439.         {
  440.             case nulDev:    cdevGlobalsHdl = DoNul(cdevGlobalsHdl);
  441.                             break;
  442.                             
  443.             case initDev:    cdevGlobalsHdl = DoInit(CPDialog,numItems);
  444.                             break;
  445.                             
  446.             case closeDev:    cdevGlobalsHdl = DoClose(cdevGlobalsHdl);
  447.                             break;
  448.  
  449.             case hitDev:    cdevGlobalsHdl = DoHit(cdevGlobalsHdl,item - numItems);
  450.                             break;
  451.                             
  452.             case updateDev:    cdevGlobalsHdl = DoUpdate(cdevGlobalsHdl);
  453.                             break;
  454.             
  455.             case activDev:    cdevGlobalsHdl = DoActive(cdevGlobalsHdl);
  456.                             break;
  457.             
  458.             case deactivDev:    
  459.                             cdevGlobalsHdl = DoDeactive(cdevGlobalsHdl);
  460.                             break;
  461.             
  462.             case keyEvtDev:
  463.                             cdevGlobalsHdl = DoKeyEvent(cdevGlobalsHdl,CPDialog,theEvent);
  464.                             break;
  465.                             
  466.             case undoDev:    /* not supported */        break;
  467.             case cutDev:    DialogCut(CPDialog);        break;
  468.             case copyDev:    DialogCopy(CPDialog);        break;
  469.             case pasteDev:    DialogPaste(CPDialog);        break;
  470.             case clearDev:    DialogDelete(CPDialog);    break;
  471.             
  472.             default:        // DebugStr("\pdefault:");
  473.                             break;
  474.         }
  475.     }
  476.  
  477. #ifdef THINK_C
  478.     RestoreA4();
  479. #endif
  480. #ifdef __MWERKS__
  481.     SetA4(oldA4);
  482. #endif
  483.     return cdevGlobalsHdl;
  484. }
  485.